import ax from 'axios'
import qs from 'qs'
import Vue from 'vue'
const ev = 'dev'
let paths = {
    dev : '//www.bbw.test/api/',
    cn : '//www.bbwgt.com.cn/api/'
}
let baseURL = paths[ev]
let axios = ax.create({
    baseURL: baseURL,
    timeout: 2000
})
const get = function (path,opt) {
    return new Promise((resolve,reject) => {
        axios.get(path,{
            params:opt
        }).then(res=>{
            let {code} = res.data
            if(code === 200){
                resolve(res.data)
            }else if(code === 701){
                // console.log(Vue)
                Vue.prototype.$message(res.data.msg)
                setTimeout(_=>{
                    localStorage.removeItem('token')
                    localStorage.removeItem('email')
                    // window.location.reload()
                },2000)
            }else if(code === 702){
                resolve(res.data)
            }else {
                reject(res.data)
            }
        }).catch(err=>{
            reject(err)
        })
    })
}
const post = function (path,opt) {
    return new Promise((resolve,reject) => {
        axios.post(path,qs.stringify(opt)).then(res=>{
            let {code} = res.data
            if(code === 200){
                resolve(res.data)
            }else if(code === 701) {
                Vue.prototype.$message(res.data.msg)
                setTimeout(_=>{
                    localStorage.removeItem('token')
                    localStorage.removeItem('email')
                    window.location.reload()
                },2000)
            }else if(code === 403){
                reject(res.data)
            }else {
                reject(res.data)
            }
        }).catch(err=>{
            reject(err)
        })
    })
}
export {
    get,
    post,
    baseURL
}
